﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rougeliek
{
    class Rougeliek
    {
        // Set some globals, constants and prototypes
        public static Player pc;
        public static Map m;
        public static Mob M; 
        public const int SCREENMIN = 1;                         // Defining constants for the screen size
        public const int SCREENX = 70;                          // allowing it to be easily changed, all ui
        public const int SCREENY = 30;                          // and maps are designed to scale.
        public const int FOLIAGE = (SCREENX * SCREENY) / 10;    // Foliage and trees are now generated based on
        public const int TREES = (SCREENX * SCREENY) / 40;      // an amount based on the size of the game area.
        public static bool gameRunning;                         // Boolean to break out of the main loop.

        static void gameInit()
        {
            // Flag the quit boolean to false, when toggled to true the loop will break and the game will quit
            gameRunning = true;

            // Make a new player and spawn with default stats
            pc = new Player();            
            pc.playerSpawn();

            // Generate map and populate with foliage based on screen size
            m = new Map();
            m.generateMap(FOLIAGE, TREES);

            M = new Mob();
            M.spawnMob();

            UI.generateUI();
            Inventory.sampleInventory();

            // Some basic window management
            Console.Title = "Rougeliek";
            Console.SetWindowSize(SCREENX+2, SCREENY+5);
            Console.SetBufferSize(SCREENX+2, SCREENY+5);
            Console.CursorVisible = false;
            
            // Draw the window after initialisation
            ScreenBuffer.initBuffer();
            ScreenBuffer.drawBuffer();
        }

        static void gameLoop()
        {
            // This is the main loop, will not break (hopefully) unless quitCommand is toggled
            while (gameRunning)
            {
                // Temporary variable to detect keystroke
                ConsoleKey inputTemp;
                inputTemp = Console.ReadKey().Key;

                // Start of command switch, parses keystrokes
                switch (inputTemp)
                {
                    // Controls to do many wonderful things!
                    case ConsoleKey.UpArrow:
                        pc.playerMove("N");
                        break;
                    
                    case ConsoleKey.RightArrow:
                        pc.playerMove("E");
                        break;

                    case ConsoleKey.DownArrow:
                        pc.playerMove("S");
                        break;

                    case ConsoleKey.LeftArrow:
                        pc.playerMove("W");
                        break;

                    case ConsoleKey.R:
                        gameInit();
                        break;

                    case ConsoleKey.I:
                        UI.menuUI("inventory");
                        break;

                    case ConsoleKey.S:
                        UI.menuUI("status");
                        break;

                    case ConsoleKey.H:
                        UI.menuUI("help");
                        break;

                    case ConsoleKey.P:
                        UI.menuUI("pause");
                        break;

                    case ConsoleKey.Q:
                        gameRunning = false;
                        break;

                    case ConsoleKey.D:
                        Inventory.addItem();
                        break;

                    default:                    // If unrecognised, break 
                        break;                  // the loop and retry
                }
                M.moveMob();
            }
        }

        static void Main(string[] args)
        {
            // Initialise the game, window, spawn player, etc
            gameInit();

            // Enter the main game loop
            gameLoop();

            // If the loop ends, quit the game
            gameQuit();
        }

        static void gameQuit()
        {
            // Called when main loop is terminated
        }
    }
}
